﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model
{
    [ObjectEvent]
    public class Reimu_C001A_CenterEvent : ObjectEvent<Reimu_C001A>, IAwake
    {
        public void Awake()
        {
            Get().Awake();
        }
    }
    /*
     * Center:防御阶段，你可以令你宣言的第一张战斗符卡获得[反制]特性
     * Hyper:战斗阶段，你宣言灵梦的战斗符卡时，威力+500*x，x为已宣言的符卡-2且至少为0
     * */
    public class Reimu_C001A : BattleEventComponent
    {
        int DefenceNum,AttackNum;
        bool IfRemove;
        int PowerAdd;
        public override BattleEventType[] EventType { get; set; } = new BattleEventType[] { BattleEventType.TurnStart, BattleEventType.TakeDefence,BattleEventType.TakeAttack, BattleEventType.FightEnd };

        public override async Task RunEvent(BattleEventType eventType, FightModel fightModel, params object[] param)
        {
            BattleCard CharactorCard = GetEntity<BattleCard>();
            BattleCard Card = null;
            if (CharactorCard.IfHyper)
            {
                Card = (param[0] as DamageInfo).AttackCard;
                if (eventType == BattleEventType.TakeAttack)
                {
                    if (Card.GetComponent<SpellCardComponent>().charactorType == CharactorType.Reimu)
                    {
                        AttackNum++;
                        PowerAdd = 500 * (AttackNum - 3);
                        if (PowerAdd <= 0) PowerAdd = 0;
                        else
                        {
                            Log.Debug($"{Card.CardName}因为[亚空连击]获得{PowerAdd}点加成");
                            Card.GetComponent<SpellCardComponent>().Power += PowerAdd;
                        }
                    }
                }
                if (eventType == BattleEventType.FightEnd)
                {
                    Card.GetComponent<SpellCardComponent>().Power -= PowerAdd;
                    Log.Debug($"power:{ Card.GetComponent<SpellCardComponent>().Power}");
                }
            }
            else if (CharactorCard.IfCenter)
            {
                if (eventType == BattleEventType.TurnStart)
                {
                    DefenceNum = 0;
                    AttackNum = 0;
                }
                if (eventType == BattleEventType.TakeDefence)
                {
                    Card = (param[0] as DamageInfo).DefenceCard;
                    DefenceNum++;
                    if (DefenceNum == 1)
                    {
                        DecideInfo decideInfo = new DecideInfo()
                        {
                            Describe = "是否发动[灵能结界]使该符卡获得反制特性",
                            CallBack = () =>
                            {
                                Features_StableComponent stableComponent = Card.GetComponent<Features_StableComponent>();
                                IfRemove = stableComponent == null;
                                if (IfRemove)
                                {
                                    Card.AddComponent<Features_StableComponent>();
                                    Log.Debug($"{Card.CardName}因为[灵能结界]的效果获得了反制效果");
                                }
                            },
                        };
                        await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.ShowWindow, fightModel, WindowType.Ask, decideInfo);
                    }
                }
                if (eventType == BattleEventType.FightEnd)
                {
                    DamageInfo damageInfo = param[0] as DamageInfo;
                    if (IfRemove) damageInfo.DefenceCard.RemoveComponent<Features_StableComponent>();
                }
            }
        }
    }
}
